package uniandes.cupi2.clienteJoy.interfaz;

import java.awt.*;
import javax.swing.border.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

public class PanelBarra extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel panelBarra;
	private int longBarra = 0;
	private ArrayList<Rectangle2D> partesB;
	private int partes;
	private JPanel panelBarra1 = null;
	
	/**
	 * This is the default constructor
	 */
	public PanelBarra() {
		super();
		panelBarra = new JPanel();
		panelBarra.setLayout(new GridBagLayout());
		panelBarra.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		panelBarra.setPreferredSize(new Dimension(25, 25));
		initialize();
	}

	/**
	 * This method initializes this
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.fill = GridBagConstraints.BOTH;
		gridBagConstraints.ipadx = 350;
		gridBagConstraints.ipady = 35;
		gridBagConstraints.gridwidth = 0;
		gridBagConstraints.gridheight = 0;
		gridBagConstraints.anchor = GridBagConstraints.EAST;
		gridBagConstraints.gridy = 0;
		GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		gridBagConstraints11.gridx = 0;
		gridBagConstraints11.insets = new Insets(0, 12, 0, 0);
		gridBagConstraints11.fill = GridBagConstraints.BOTH;
		gridBagConstraints11.anchor = GridBagConstraints.SOUTH;
		gridBagConstraints11.gridwidth = 0;
		gridBagConstraints11.gridheight = 1;
		gridBagConstraints11.ipadx = 21;
		gridBagConstraints11.ipady = 20;
		gridBagConstraints11.gridy = 0;
		this.setSize(463, 74);
		this.setPreferredSize( new Dimension( 400, 10 ) );
		this.setLayout(new GridBagLayout());
		this.add(getPanelBarra1(), gridBagConstraints);
		this.add(panelBarra, gridBagConstraints11);
		partesB = new ArrayList<Rectangle2D>();
	}
	
	public void particionarBarra(int npartes )
	{
		partes=npartes;
		longBarra=panelBarra.getWidth();
		double xi=0;
		double yi=0;
		double alto=panelBarra.getHeight();
		double incrementoAncho=longBarra/(double)partes;
		double mAncho=incrementoAncho;
		for( int i = 0 ; i < partes ; i++ )
		{
			Rectangle2D r = new Rectangle2D.Double( xi, yi, mAncho, alto );
			partesB.add(r);
			xi+=incrementoAncho;
		}
		update((Graphics2D)panelBarra1.getGraphics());
	}
	


	
	public void update( Graphics2D g )
	{
		 g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
	     g.setColor( getBackground( ) );
	     g.fillRect( 0, 0, getWidth( ), getHeight( ) );
	     
	        for(int i=0; i<partesB.size(); i++)
	        {
	        	Rectangle2D rectangulo = (Rectangle2D)partesB.get(i);
	        	System.out.println(rectangulo);
	        	g.setPaint( Color.RED );
	          	g.fill( rectangulo );
	        }

	}

	public void colorearFragmento(int fragmento) 
	{
		Rectangle2D rectangulo = (Rectangle2D)partesB.get(fragmento-1);

    	Graphics2D g = (Graphics2D)panelBarra.getGraphics();
    	g.setPaint( Color.BLUE );
    	g.fill( rectangulo );
	}

	/**
	 * This method initializes panelBarra1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanelBarra1() {
		if (panelBarra1 == null) {
			panelBarra1 = new JPanel();
			panelBarra1.setLayout(new GridBagLayout());
			panelBarra1.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));
			panelBarra1.setPreferredSize(new Dimension(150, 150));
		}
		return panelBarra1;
	}

}  //  @jve:decl-index=0:visual-constraint="39,46"
